package com.deyuanyun.pic.ctrl.service.impl;

import com.deyuanyun.pic.common.spring.SpringAppliction;
import com.deyuanyun.pic.common.tool.JXL;
import com.deyuanyun.pic.common.util.StringUtils;
import com.deyuanyun.pic.ctrl.controller.vo.query.EqptExceptionVO;
import com.deyuanyun.pic.ctrl.controller.vo.RecodeExceptionVO;
import com.deyuanyun.pic.ctrl.domain.RecodeException;
import com.deyuanyun.pic.ctrl.mapper.EqptRecodeExceptionAndRectifyMapper;

import com.deyuanyun.pic.ctrl.mapper.RecodeExceptionMapper;
import com.deyuanyun.pic.ctrl.service.EqptRecodeExceptionService;
import com.deyuanyun.pic.settings.controller.pipe.vo.PipeLocationDTO;
import com.deyuanyun.pic.settings.service.pipe.PipeLocationService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * 
 * @author lijun
 * @date 2016-09-06
 * 设备运行记录业务层
 * History: [修改人][时间][修改描述]
 */
@Service
public class EqptRecordExceptionServiceImpl implements EqptRecodeExceptionService {

    @Resource
    private EqptRecodeExceptionAndRectifyMapper eqptRecodeExceptionAndRectifyMapper;
    @Resource
    private RecodeExceptionMapper recodeExceptionMapper;
    @Resource
    private PipeLocationService pipeLocationService;


    @Override
    public List<RecodeExceptionVO> queryRecodeException(EqptExceptionVO exception) {

        Long beginMileage = null,endMileage = null;
        if(StringUtils.isNotNullEmpty(exception.getPipeLineId()) && StringUtils.isNotNullEmpty(exception.getBeginId())){
            PipeLocationDTO pipeLocation = pipeLocationService.queryPipeLocationById(exception.getPipeLineId(),exception.getBeginId());

            beginMileage = pipeLocation != null? pipeLocation.getMileage2():beginMileage;
            exception.setBeginMileage(beginMileage != null ? new BigDecimal(beginMileage) : null);
        }
        if(StringUtils.isNotNullEmpty(exception.getPipeLineId()) && StringUtils.isNotNullEmpty(exception.getEndId())){
            PipeLocationDTO pipeLocation = pipeLocationService.queryPipeLocationById(exception.getPipeLineId(),exception.getEndId());

            endMileage = pipeLocation != null? pipeLocation.getMileage2():endMileage;
            exception.setEndMileage(endMileage != null ? new BigDecimal(endMileage) : null);
        }


        return eqptRecodeExceptionAndRectifyMapper.queryRecodeException(exception);
    }

    @Override
    public RecodeExceptionVO queryRecodeExceptionById(String exceptionId) {
        return eqptRecodeExceptionAndRectifyMapper.queryRecodeExceptionById(exceptionId);
    }

    @Override
    public List<String[]> queryDataExport(EqptExceptionVO exception) {
        List<RecodeExceptionVO> recodeExceptionList= this.queryRecodeException(exception);
        List<String[]> lsr = JXL.toListFormVO(recodeExceptionList, "yyyy-MM-dd");
        return lsr;
    }

    @Override
    public int updteEqptRecoedException(RecodeException exception) {
        return recodeExceptionMapper.updateByPrimaryKeySelective(exception);
    }

}
